
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "led.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_ADDRESS		0x4001244C
/* Private macro -------------------------------------------------------------*/
#define ADC_TO_mVOLT(a)		(u16)(3300 * (u32)a / 4095)
/* Private variables ---------------------------------------------------------*/
static u16 gDmaBuf[ADC_MAX] = {0,};
static u32 gAdcCount = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


void Adc_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure = {0,};
	NVIC_InitTypeDef NVIC_InitStructure = {0,};


	/* GPIOB Periph clock enable */
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

	/* Configure ADC Channel[2:0] as analog input */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_1 | GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOA, &GPIO_InitStructure);


	/* DMA1 clock enable */
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	/* ADC1 Periph clock enable */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);


	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

void Adc_Proc(u32 mode, u32 modeChanged)
{
	if (modeChanged) {
	}

	if (gAdcCount > 500000) {
		gAdcCount = 0;

		u16 adcVolt[ADC_MAX];
		for (u32 i=0; i<ADC_MAX; i++) {
			adcVolt[i] = ADC_TO_mVOLT(gDmaBuf[i]);
		}
		sprintf(gszLine, " ADC:%5d%5d%5d", adcVolt[0], adcVolt[1], adcVolt[2]);
		LcdLineString(17, LCD_ALIGN_LEFT, gszLine, COLOR_RED, COLOR_BLACK);
	}
}

void Adc_Start(void)
{
	DMA_InitTypeDef DMA_InitStructure = {0,};
	ADC_InitTypeDef ADC_InitStructure = {DISABLE,};

	/* DMA1 Channel1 Config */
	DMA_DeInit(DMA1_Channel1);
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
	DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)gDmaBuf;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStructure.DMA_BufferSize = _countof(gDmaBuf);
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);

	/* DMA1 Channel1 enable */
	DMA_Cmd(DMA1_Channel1, ENABLE);


	/* ADC1 DeInit */
	ADC_DeInit(ADC1);

	/* Enable ADC_DMA */
	ADC_DMACmd(ADC1, ENABLE);

	/* Initialize ADC structure */
	ADC_StructInit(&ADC_InitStructure);
	/* ADC1 Configuration ------------------------------------------------------*/
	ADC_InitStructure.ADC_ScanConvMode = ENABLE;
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStructure.ADC_NbrOfChannel = ADC_MAX;
	ADC_Init(ADC1, &ADC_InitStructure);

	/* Convert the ADC1 Channel[2:0] with 13.5 Cycles as sampling time */
	u8 ADC_SampleTime = ADC_SampleTime_13Cycles5;	// ADC_SampleTime_13Cycles5
	ADC_RegularChannelConfig(ADC1, ADC_Channel_0,  1, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_1,  2, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_2,  3, ADC_SampleTime);

	/* Enable ADC1 */
	ADC_Cmd(ADC1, ENABLE);
	ADC_ResetCalibration(ADC1);
	while (ADC_GetResetCalibrationStatus(ADC1));
	ADC_StartCalibration(ADC1);
	while (ADC_GetCalibrationStatus(ADC1));

	/* Enable ADC1 */
	ADC_Cmd(ADC1, ENABLE);

	gAdcCount = 0;

	Led_Set(LED_GREEN, 1);
}
void Adc_Stop(void)
{
	/* Disable ADC1 */
	ADC_Cmd(ADC1, DISABLE);

	/* DMA1 Channel1 disable */
	DMA_Cmd(DMA1_Channel1, DISABLE);

	for (u32 i=0; i<ADC_MAX; i++) {
		gDmaBuf[i] = 0;
	}

	Led_Set(LED_GREEN, 0);
}

u32 Adc_Get(u32 adc)
{
	if (ADC_MAX > adc) {
		return gDmaBuf[adc];
	}

	return (u32)-1;
}
u32 Adc_Get_mV(u32 adc)
{
	if (ADC_MAX > adc) {
		return ADC_TO_mVOLT(gDmaBuf[adc]);
	}

	return (u32)-1;
}


void DMA1_Channel1_IRQHandler(void)
{
	//if (DMA_GetITStatus(DMA1_IT_TC1)) {
	//	DMA_ClearITPendingBit(DMA1_IT_TC1);
	if (DMA1->ISR & DMA1_IT_TC1) {
		DMA1->IFCR = DMA1_IT_TC1;

		gAdcCount++;
	}
}

